﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class MyImage : MaskableGraphic
{
    public Transform image;
    [SerializeField]RectTransform RectTransform;

    Vector2 leftBottomPos;
    Vector2 leftTopPos;
    Vector2 rightTopPos;
    Vector2 rightBottomPos;
    

    Vector2 screen_leftBottomPos = new Vector3(-Screen.width * 0.5f, -Screen.height * 0.5f);
    Vector2 screen_leftTopPos = new Vector3(-Screen.width * 0.5f, Screen.height * 0.5f);
    Vector2 screen_rightTopPos = new Vector3(Screen.width * 0.5f, Screen.height * 0.5f);
    Vector2 screen_rightBottomPos = new Vector3(Screen.width * 0.5f, -Screen.height * 0.5f);

    Vector2 size;

    bool Init = false;
  

    public void OnEnter()
    {
        if(!Init)
        {
            Init = true;
            size = rectTransform.rect.size;
            screen_leftBottomPos = new Vector3(0, 0);
            screen_leftTopPos = new Vector3(0, size.y);
            screen_rightTopPos = new Vector3(size.x, size.y);
            screen_rightBottomPos = new Vector3(size.x, 0);
        }
        gameObject.SetActive(true);
        RectTransform.gameObject.SetActive(true);
        Vector2 s_pos = new Vector2(0, size.y);
        Vector2 e_pos = new Vector2(size.x, 0);
        Set(MonoTool.Instance.ToLocalPos(s_pos), MonoTool.Instance.ToLocalPos(new Vector2(e_pos.x, s_pos.y)), MonoTool.Instance.
            ToLocalPos(e_pos), MonoTool.Instance.ToLocalPos(new Vector2(s_pos.x, e_pos.y)), s_pos, e_pos);
    }


    public void OnExit()
    {
        gameObject.SetActive(false);
        RectTransform.gameObject.SetActive(false);
    }


    public void UpdateData(Vector3 s_pos,Vector3 e_pos)
    {
        Set(MonoTool.Instance.ToLocalPos(s_pos), MonoTool.Instance.ToLocalPos(new Vector2(e_pos.x, s_pos.y)), MonoTool.Instance.
            ToLocalPos(e_pos), MonoTool.Instance.ToLocalPos(new Vector2(s_pos.x, e_pos.y)), s_pos,e_pos);
        //Set(MonoTool.Instance.ToLocalPos(s_pos), MonoTool.Instance.ToLocalPos(new Vector2(e_pos.x, s_pos.y)), MonoTool..ToLocalPos(e_pos), new Vector2(s_pos.x, e_pos.y));
    }

    void Set(Vector2 leftTopPos, Vector2 rightTopPos, Vector2 rightBottomPos, Vector2 leftBottomPos,Vector2 screenstartPos, Vector2 screenendPos)
    {
        this.leftTopPos = screenstartPos;
        this.rightTopPos = new Vector2(screenendPos.x,screenstartPos.y);
        this.rightBottomPos = screenendPos;
        this.leftBottomPos = new Vector2(screenstartPos.x, screenendPos.y);

        float width = Mathf.Abs(rightBottomPos.x - leftBottomPos.x);
        float height = Mathf.Abs(leftTopPos.y - leftBottomPos.y);
        RectTransform.transform.localPosition =  leftBottomPos-new Vector2(transform.localPosition.x, transform.localPosition.y);//(leftTopPos + rightBottomPos) / 2;
        RectTransform.sizeDelta=new Vector2(width,height);
        
        SetVerticesDirty();
    }


    

    protected override void OnPopulateMesh(VertexHelper vh)
    {
        vh.Clear();

        UIVertex[] verts0 = new UIVertex[4];

        //顶部
        verts0[0] = SetVertexs(verts0[0], leftTopPos);
        verts0[1] = SetVertexs(verts0[1], screen_leftTopPos);
        verts0[2] = SetVertexs(verts0[2], screen_rightTopPos);
        verts0[3] = SetVertexs(verts0[3], rightTopPos);

        vh.AddUIVertexQuad(verts0);

        UIVertex[] verts1 = new UIVertex[4];
        //右侧
        verts1[0] = SetVertexs(verts1[0], rightBottomPos);
        verts1[1] = SetVertexs(verts1[1], rightTopPos);
        verts1[2] = SetVertexs(verts1[2], screen_rightTopPos);
        verts1[3] = SetVertexs(verts1[3], screen_rightBottomPos);
        vh.AddUIVertexQuad(verts1);

        //底部
        UIVertex[] verts2 = new UIVertex[4];
        verts2[0] = SetVertexs(verts2[0], screen_leftBottomPos);//屏幕左下角
        verts2[1] = SetVertexs(verts2[1], leftBottomPos);//屏幕左上角
        verts2[2] = SetVertexs(verts2[2], rightBottomPos);//屏幕右上角
        verts2[3] = SetVertexs(verts2[3], screen_rightBottomPos);//屏幕右下角
        vh.AddUIVertexQuad(verts2);

        //左侧
        UIVertex[] verts3 = new UIVertex[4];
        verts3[0] = SetVertexs(verts3[0], screen_leftBottomPos);
        verts3[1] = SetVertexs(verts3[1], screen_leftTopPos);
        verts3[2] = SetVertexs(verts3[2], leftTopPos);
        verts3[3] = SetVertexs(verts3[3], leftBottomPos);
        vh.AddUIVertexQuad(verts3);
    }
    

    UIVertex SetVertexs(UIVertex vertex, Vector2 vertexPos)
    {
        vertex.position = vertexPos;
        vertex.color = color;//color是父类中的属性
        vertex.uv0 = Vector2.zero;
        return vertex;
    }
}
